<!--
 * @Author: xiaodong 565071984@qq.com
 * @Date: 2022-05-08 19:52:05
 * @LastEditors: xiaodong 565071984@qq.com
 * @LastEditTime: 2022-05-17 20:54:28
 * @FilePath: /小票/src/views/accountManagement/userManagement/components/UserManagementEdit.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogFormVisible"
    width="500px"
    :append-to-body="true"
    @close="close"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <!-- <el-form-item label="代理商账号" prop="account">
        <el-input
          v-model.trim="form.account"
          autocomplete="off"
          disabled
        ></el-input>
      </el-form-item> -->
      <template v-if="isNewBuy">
        <el-radio-group v-model="radio" @change="onChangeRadio">
          <el-radio
            v-for="(item, index) in tanantList"
            :key="index"
            :label="item.da_id"
          >
            {{ item.name }}
          </el-radio>
        </el-radio-group>
      </template>
      <template v-else>
       <el-checkbox-group v-model="checkList" @change="onChangeCheck">
          <el-checkbox
            v-for="(item, index) in tanantList"
            :key="index"
            :label="item.name"
          ></el-checkbox>
        </el-checkbox-group>
      </template>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">
        {{ $t('common.cancel') /*取 消*/ }}
      </el-button>
      <el-button type="primary" @click="save">
        {{ $t('common.sure') /*确 定*/ }}
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
   import { getDevList } from '@/api/business.js'
  export default {
    name: 'UserManagementEdit',
    props: {
      tanantList: {
        type: Array,
        default: function () {
          return []
        },
      },
      modyType: {
        type: String,
        default: function () {
          return ''
        },
      },
    },
    data() {
      return {
        radio: '',
        checkList: [],
        form: {
          username: '',
          password: '',
          email: '',
          permissions: [],
        },
        rules: {
          username: [
            { required: true, trigger: 'blur', message: '请输入用户名' },
          ],
          password: [
            { required: true, trigger: 'blur', message: '请输入密码' },
          ],
          email: [{ required: true, trigger: 'blur', message: '请输入邮箱' }],
          permissions: [
            { required: true, trigger: 'blur', message: '请选择权限' },
          ],
        },
        title: '',
        dialogFormVisible: false,
        isNewBuy: true,
        selAccounts: [],
      }
    },
    created() {},
    methods: {
         getTDevList(){
           let rq={
           }
          getDevList(rq).then(res=>{
            if(res.code==0){
              this.selAccounts=res.data;
            }
          }) 
      },
      onChangeRadio(label) {
        const findItem = this.tanantList.find((item) => item.da_id === label)
        this.selAccounts = [findItem]
        // console.log(this.selAccounts)
      },
      onChangeCheck(label) {
        console.log(label)
        const findItem = this.tanantList.filter((item) =>
          label.includes(item.name)
        )
        this.selAccounts = [...findItem]
      },
      showEdit(row, isNewBuy) {
        if (!row) {
          this.title =
            this.modyType === '租户'
              ? this.$t('accountManage.selAgentAccount') /*选择租户账号*/
              : this.$t('accountManage.selDeviceAccount') /*选择设备账号*/
        } else {
          this.title = '编辑'

          this.checkList=row
         
        }
       
        this.isNewBuy = isNewBuy //是否新购
        if (isNewBuy) {
          this.radio = this.tanantList[0]?.da_id //新购默认第一个
          console.log(isNewBuy, '是否新购', this.radio)
        }
        this.dialogFormVisible = true
        this.getTDevList();
      },
      close() {
        this.$refs['form'].resetFields()
        this.form = this.$options.data().form
        this.dialogFormVisible = false
      },
      save() {
    
        this.$emit('set-account', this.checkList)
        this.close()
      },
    },
  }
</script>
